package com.syaccp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syaccp.mapper.WithdrawalDetailsMapper;
import com.syaccp.model.Customer;
import com.syaccp.model.SysDictData;
import com.syaccp.model.SysUser;
import com.syaccp.model.WithdrawalDetails;
import com.syaccp.request.WithdrawalRequest;
import com.syaccp.service.CustomerService;
import com.syaccp.service.DictDataService;
import com.syaccp.service.WithdrawalDetailsService;
import com.syaccp.utils.Result;
import com.syaccp.utils.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class WithdrawalDetailsServiceImpl  extends ServiceImpl<WithdrawalDetailsMapper, WithdrawalDetails> implements WithdrawalDetailsService {

    @Resource
    private CustomerService customerService;

    @Resource
    private DictDataService dictDataService;







    @Override
    public Result withdrawDeposit(WithdrawalRequest withdrawalRequest) {
        Customer customer = customerService.getBaseMapper().selectById(withdrawalRequest.getCustomerId());
        SysDictData sysDictData = dictDataService.getBaseMapper().selectById(withdrawalRequest.getBankId());
        WithdrawalDetails wd=new WithdrawalDetails();
        //提现金额
        wd.setMoney(withdrawalRequest.getMoney());
        //手续费1/1000,不够1元的按1元计算
        wd.setServiceMoney(new Double(Math.ceil(withdrawalRequest.getMoney()/1000)).intValue());
        //提现时间
        wd.setWithdrawTime(new Date());
        //提现申请人
        wd.setCustomerId(customer.getId());
        wd.setCustomerName(customer.getCustomerName());
        wd.setCustomerPicture(customer.getWechatImage());
        //银行信息
        wd.setBankId(sysDictData.getId());
        wd.setBankName(sysDictData.getLabel());
        wd.setBankAccount(withdrawalRequest.getBankAccount());
        //开户人信息
        wd.setAccountName(withdrawalRequest.getAccountName());
        wd.setAccountPhone(withdrawalRequest.getAccountPhone());
        int insert = this.baseMapper.insert(wd);
        //新增失败
        if (insert==0){
            return Result.error();
        }
        return Result.success(insert);
    }
}
